package project.ccso.top.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import project.ccso.top.dao.AdminDao;
import project.ccso.top.entity.Admin;
import project.ccso.top.util.DBUtil;

/**
 * 管理员Dao的实现类
 * @author aibinxiao
 * @date 2018年8月23日 上午10:27:32
 */
public class AdminDaoImpl implements AdminDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(AdminDaoImpl.class);
	// 注入DBUtil
	private DBUtil dbUtil = new DBUtil();

	/**
	 * 1.管理员登陆
	 */
	@Override
	public Admin login(String loginName, String password) {
		String sql = "select * from t_admin where ad_name = ? and ad_password = ?";
		ResultSet rs = dbUtil.executeQuery(sql, new Object[] {loginName, password});
		Admin admin = null;
		try {
			if(rs.next()) {
				admin = new Admin(rs.getInt(1), rs.getString(2), rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return admin;
	}

	
	/**
	 * 根据id查询管理员
	 * @param parseInt
	 * @return
	 */
	@Override
	public Admin findById(int id) {
		String sql = "select * from t_admin where ad_id = ?";
		ResultSet rs = dbUtil.executeQuery(sql, new Object[] {id});
		Admin admin = null;
		try {
			if(rs.next()) {
				admin = new Admin(rs.getInt(1), rs.getString(2), rs.getString(3));
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return admin;
	}
}
